#include "GameTimeAttack.h"
#include "ProfileState.h"
#include "MenuState.h"
#include "WorldState.h"
#include "CreditsState.h"

#include <Windows.h>

#include "..\Game.h"
#include "..\Game Objects\Projectile.h"
#include "..\Game Objects\DestructableObject.h"
#include "..\Game Objects\TriggerObject.h"
#include "..\Game Objects\Event.h"
#include "..\Game Objects\Player.h"
#include "..\Game Objects\Enemy.h"
#include "..\Game Objects\Boss.h"
#include "..\Game Objects\Camera.h"
#include "..\Game Objects\PowerUps.h"
#include "..\Game Objects\CheckPointObject.h"
#include "..\Game Objects\CrumblingPlatformObject.h"
#include "..\Game Objects\WallTriggerObject.h"
#include "..\Game Objects\SwitchTriggerObject.h"
#include "..\Game Objects\SpikeObject.h"
#include "..\Game Objects\SpringboardObject.h"
#include "..\Sprite Management\Animation.h"
#include "..\Tile Management\Layer.h"
#include "..\Tile Management\Tile.h"
#include "..\Tile Management\TileManager.h"

#include "..\Bitmap Font\BitmapFont.h"

#include "..\..\SGD Wrappers\SGD_AudioManager.h"
#include "..\..\SGD Wrappers\SGD_GraphicsManager.h"
#include "..\..\SGD Wrappers\SGD_InputManager.h"
#include "..\..\SGD Wrappers\SGD_Geometry.h"

#include "..\..\TinyXML\tinyxml2.h"

#include <sstream>

GameTimeAttack* GameTimeAttack::GetInstance(void)
{
	static GameTimeAttack s_Instance;	// stored in global memory once
	return &s_Instance;
}

void GameTimeAttack::Enter()
{
	///////////////////////////////////////////////
	// Found Bug Fix ID: 31
	// Implemented by: Cliff Baird
	// Set checkpoint flag to false on loading level
	///////////////////////////////////////////////
	GAME->SetCheckpoint(false);
	///////////////////////////////////////////////
	// End Found Bug Fix ID: 31
	//////////////////////////////////////////////

	GAME->PauseTimeAttack(true);
	SetTransition(true);
	SetTransitionID(START);
	SetFadeoutDuration(3.0f);
	ResetFadeoutTimer();
	IsGameOver(false);
	GAME->SetCheckpoint(false);

	SGD::GraphicsManager* Graphics = SGD::GraphicsManager::GetInstance();

	GAME->PlayBGM(false);

	// Load From XML
	if (!LoadLevel())
	{
	}

	// Load Pause Image
	hPauseBackground = Graphics->LoadTexture(L"Resource/Images/PauseMenu.png");

	// Load Sword End Image
	hSword = GRAPHICS->LoadTexture(L"Resource/Images/swordend.png");

	// Set Cursor
	nCursor = 0;

	// Set Game Paused
	bGamePaused = false;

	// Configure Camera
	Camera * camera = Camera::GetInstance();
	camera->SetPosition(SGD::Point(0.0f, 0.0f));
	camera->SetFollowing(true);
	camera->SetSpeed(0.0f);
	camera->SetMaxSpeed(50.0f);

	fVictory = 3.0f;
	bVictory = false;

	// Moves camera to player at spawn (for intro slides)
	GAME->GetPlayer()->Update(0.0f);
}

void GameTimeAttack::Exit()
{
	SGD::GraphicsManager* Graphics = SGD::GraphicsManager::GetInstance();
	Graphics->UnloadTexture(hPauseBackground);
	Graphics->UnloadTexture(hBackground);
	GRAPHICS->UnloadTexture(hSword);
	GAME->PlayBGM(false);

	GAME->GetTileManager()->Clear();

	// Unload Objects
	GAME->GetEntityManager().RemoveAll();
}

bool GameTimeAttack::Update(float dt)
{
	if (!GAME->IsTimeAttackPaused())
		GAME->SetTimeAttackTime(GAME->GetTimeAttackTime() - dt);

	TileManager* tileManager = GAME->GetTileManager();
	SGD::InputManager* Input = SGD::InputManager::GetInstance();

	if (GetTransition()) // Transitioning between levels, lives, etc...
	{
		SetFadeoutTimer(GetFadeoutTimer() + dt);
		if (IsFadeoutFinished())
		{
			switch (GetTransitionID())
			{
			case DEATH:
				PlayerDeath();
				SetTransition(false);
				break;
			case LOSE:
				GAME->ChangeState(MenuState::GetInstance());
				break;
			case WIN:
				NextLevel();
				break;
			case START:
				SetTransition(false);
				GAME->PauseTimeAttack(false);
				SetTransitionID(-1);
				break;
			}
		}
	}
	else if (bGamePaused)
	{
		if (GamePause())
			return true;
	}
	else
	{
		/*********************************************/
		// ALL GAME UPDATE CODE GOES HERE
		// INSIDE OF THIS ELSE CONDITION
		// NO WHERE ELSE
		// OTHERWISE PAUSE MENU WILL CEASE
		// TO FUNCTION
		// WE "DO NOT" WANT THAT TO HAPPEN

		if (GAME->GetTimeAttackTime() <= 0.0f)
		{
			PlayLoseTransition();
			ResetFadeoutTimer();
			return true;
		}

		if (KEYESCAPE && !bGamePaused)
		{
			GAME->PauseTimeAttack(true);
			bGamePaused = true;
		}
		// Update
		switch (GAME->GetEntityManager().UpdateAll(dt))
		{
		case 1: // Player died
		{
			PlayDeathTransition();
			ResetFadeoutTimer();
			return true;
		}
		case 2: // Player died and has no lives remaining
		{
			PlayLoseTransition();
			ResetFadeoutTimer();
			return true;
		}
		case 3: // Player beat boss
		{
			GAME->PauseTimeAttack(true);
			PlayWinTransition();
			ResetFadeoutTimer();
			return true;
		}
		}

		// Check Collisions
		tileManager->CheckCollisions(0);
		//  - Player to Enemies
		GAME->GetEntityManager().CheckCollisions(0, 1);
		//  - Player to Boss
		GAME->GetEntityManager().CheckCollisions(0, 2);
		//  - Player to PowerUps
		GAME->GetEntityManager().CheckCollisions(0, 3);
		//  - Player to Events
		GAME->GetEntityManager().CheckCollisions(0, 5);
		//  - Player to Walls
		GAME->GetEntityManager().CheckCollisions(0, 7);
		//  - Player to Switchs
		GAME->GetEntityManager().CheckCollisions(0, 8);
		//  - Player to Crumbling Platforms
		GAME->GetEntityManager().CheckCollisions(0, 9);
		//  - Player to Checkpoint
		GAME->GetEntityManager().CheckCollisions(0, 10);
		//  - Player to Spike
		GAME->GetEntityManager().CheckCollisions(0, 11);
		//  - Player to Springboard
		GAME->GetEntityManager().CheckCollisions(0, 12);
	}
	return true;
}

void GameTimeAttack::Render()
{
	TileManager* tileManager = GAME->GetTileManager();

	RenderBackground();

	tileManager->Render();

	GAME->GetEntityManager().RenderAll();

	if (GetTransitionID() == START && GetTransition())
		RenderLevelStart();

	if (bGamePaused)
		RenderPauseMenu();

	if ((GetTransitionID() == DEATH || GetTransitionID() == LOSE || GetTransitionID() == WIN) && GetTransition())
		RenderFadeout();

	if (bVictory)
		RenderVictory();

	if (IsGameOver())
		RenderGameOver();

	RenderTimer();
}

bool GameTimeAttack::LoadLevel()
{
	TileManager* tileManager = GAME->GetTileManager();
	int curLev = GAME->GetTimeAttackLevel();
	if (curLev == 1)
	{
		GAME->SetBGM("Resource/Audio/level1bgmusic.xwm");
		tileManager->Load("Resource/hills.xml");
		GAME->SetCurrentObjects("Resource/hills.ObjXml");
		hBackground = SGD::GraphicsManager::GetInstance()->LoadTexture("Resource/Images/RollingHills.png");
		GAME->PlayBGM();
	}
	else if (curLev == 2)
	{
		GAME->SetBGM("Resource/Audio/level2bgmusic.xwm");
		tileManager->Load("Resource/forestlevel.xml");
		GAME->SetCurrentObjects("Resource/forestlevel.ObjXml");
		hBackground = SGD::GraphicsManager::GetInstance()->LoadTexture("Resource/Images/Forest.png");
		GAME->PlayBGM();
	}
	else if (curLev == 3)
	{
		GAME->SetBGM("Resource/Audio/level3bgmusic.xwm");
		tileManager->Load("Resource/Desert.xml");
		GAME->SetCurrentObjects("Resource/Desert.ObjXml");
		hBackground = SGD::GraphicsManager::GetInstance()->LoadTexture("Resource/Images/Desert.png");
		GAME->PlayBGM();
	}
	else if (GAME->GetTimeAttackLevel() == 4)
	{
		GAME->SetBGM("Resource/Audio/level4bgmusic.xwm");
		tileManager->Load("Resource/CaveFun.xml");
		GAME->SetCurrentObjects("Resource/CaveFun.ObjXml");
		hBackground = SGD::GraphicsManager::GetInstance()->LoadTexture("Resource/Images/Cave.png");
		GAME->PlayBGM();
	}
	LoadObjects(GAME->GetCurrentObjects());
	return true;
}

void GameTimeAttack::LoadSaveInformation(const char* cSaveLoc, const char cSaveLevel, const bool bSaveHalfWay, const bool bSaveVilliger1,
	const bool bSaveVilliger2, const bool bSaveVilliger3)
{
	if (cSaveLevel != '-')
	{
		cLoc = cSaveLoc;
		cLevel = cSaveLevel;
		bHalfWay = bSaveHalfWay;
		bVilliger1 = bSaveVilliger1;
		bVilliger2 = bSaveVilliger2;
		bVilliger3 = bSaveVilliger3;
	}
	else
	{
		cLoc = cSaveLoc;
		cLevel = '1';
		bHalfWay = 0;
		bVilliger1 = 0;
		bVilliger2 = 0;
		bVilliger3 = 0;
	}
}

bool GameTimeAttack::GamePause()
{
	SGD::InputManager* Input = SGD::InputManager::GetInstance();

	if (bInPauseMenu)
	{
		if (KEYESCAPE)
			nCursor = 1;
	}
	// Move cursor
	if (KEYDOWNPRESSED)
	{
		GAME->SetInput(false);
		++nCursor;

		// Wrap around
		if (nCursor > 1)
			nCursor = 0;
	}
	else if (KEYUPPRESSED)
	{
		GAME->SetInput(false);
		--nCursor;

		// Wrap around
		if (nCursor < 0)
			nCursor = 1;
	}

	// Select an option?
	if (KEYENTER)
	{
		// Which option?
		if (nCursor == 0)
		{
			GAME->PauseTimeAttack(false);
			bGamePaused = false;
			bInPauseMenu = false;
		}
		else if (nCursor == 1)
		{
			// Go To Main Menu
			GAME->ChangeState(MenuState::GetInstance());
			GAME->SetCheckpoint(false);

			// Exit
			return true;
		}
	}

	return false;
}

void GameTimeAttack::LoadObjects(const char* fileName)
{
	SetVictory(false);



	tinyxml2::XMLDocument doc;

	if (tinyxml2::XML_NO_ERROR == doc.LoadFile(fileName))
	{
		TileManager* tileManager = GAME->GetTileManager();
		tinyxml2::XMLElement* pRoot = doc.RootElement();

		if (pRoot != nullptr)
		{
			tinyxml2::XMLElement* pObject = pRoot->FirstChildElement("Object");

			// Setup WallTriggerObject vector
			std::vector<WallTriggerObject*> wallTriggerObj;

			while (pObject)
			{
				int locX = pObject->IntAttribute("X");
				int locY = pObject->IntAttribute("Y");

				SGD::Point loc = SGD::Point{ (float)locX * tileManager->GetTileSize().width, (float)locY * tileManager->GetTileSize().height };

				std::string type;
				type = pObject->Attribute("Type");

				if (type == "PLAYER_OBJ")
				{
					GAME->CreatePlayer(loc);
				}
				else if (type == "MELEE_GRUNT_OBJ")
				{
					GAME->CreateEnemy(loc, GameObject::ObjectID::MELEE);
				}
				else if (type == "MAGE_GRUNT_OBJ")
				{

				}
				else if (type == "RANGED_GRUNT_OBJ")
				{
					GAME->CreateEnemy(loc, GameObject::ObjectID::RANGED);
				}
				else if (type == "EVENT")
				{
					GAME->CreateEventt(loc);
				}
				else if (type == "CHECK_POINT_OBJ")
				{
					GAME->CreateCheckPoint(loc, GameObject::ObjectID::CHECK_POINT);

					// Check if player is at check point - if yes, change position to checkpoint
					if (GAME->GetCheckpoint())
						GAME->GetPlayer()->SetPosition(loc);
				}
				else if (type == "HEALTH_POWERUP_OBJ")
				{
					GAME->CreateHPPowerUp(loc, GameObject::ObjectID::POWERUP_HEALTH);
				}
				else if (type == "SPEED_POWERUP_OBJ")
				{
					GAME->CreateSpeedPowerUp(loc, GameObject::ObjectID::POWERUP_SPEED);
				}
				else if (type == "DAMAGE_POWERUP_OBJ")
				{
					GAME->CreateDamagePowerUp(loc, GameObject::ObjectID::POWERUP_DAMAGE);
				}
				else if (type == "CRUMBLING_PLATFORM_OBJ")
				{
					GAME->CreateCrumblingPlatform(loc, GameObject::ObjectID::CRUMBLING_PLATFORM);
				}
				else if (type == "WALL_TRIGGER_OBJ")
				{
					// Check for owner's ID
					int ownerID = pObject->IntAttribute("OwnerID");

					WallTriggerObject* trigger = GAME->CreateWallTriggerObject(loc, GameObject::ObjectID::WALL_TRIGGER_OBJECT, (unsigned int)ownerID);

					// Add wall trigger objects to vector for switch trigger access
					wallTriggerObj.push_back(trigger);
				}
				else if (type == "SWITCH_TRIGGER_OBJ")
				{
					// Check for target's ID
					int targetID = pObject->IntAttribute("TargetID");

					GAME->CreateSwitchTrigger(loc, GameObject::ObjectID::SWITCH_TRIGGER, (unsigned int)targetID);
				}
				else if (type == "SPIKE_OBJ")
				{
					GAME->CreateSpike(loc, GameObject::ObjectID::SPIKE);
				}
				else if (type == "SPRINGBOARD_OBJ")
				{
					GAME->CreateSpringboard(loc, GameObject::ObjectID::SPRINGBOARD);
				}
				else if (type == "BOSS_OBJ")
				{
					GAME->CreateBoss(loc, GameObject::ObjectID::BOSS, GAME->GetTimeAttackLevel());
				}

				pObject = pObject->NextSiblingElement("Object");
			}
			// Store wall trigger objects inside of Game
			GAME->SetWallTriggerObjectsVector(wallTriggerObj);
		}
	}

	int curLev = GAME->GetTimeAttackLevel();
	for (unsigned int i = 0; i < GAME->GetTileManager()->vLiquids0.size(); ++i)
	{
		if (curLev == 1)
			GAME->CreateSurfaceLiquid(GAME->GetTileManager()->vLiquids0[i], 0, curLev);
		else if (curLev == 2)
			GAME->CreateSurfaceLiquid(GAME->GetTileManager()->vLiquids0[i], 2, curLev);
		else if (curLev == 3)
			GAME->CreateSurfaceLiquid(GAME->GetTileManager()->vLiquids0[i], 0, curLev);
		else if (curLev == 4)
			GAME->CreateSurfaceLiquid(GAME->GetTileManager()->vLiquids0[i], 10000, curLev);
	}
	for (unsigned int i = 0; i < GAME->GetTileManager()->vLiquids1.size(); ++i)
	{
		if (curLev == 1)
			GAME->CreateLiquid(GAME->GetTileManager()->vLiquids1[i], 0, curLev);
		else if (curLev == 2)
			GAME->CreateLiquid(GAME->GetTileManager()->vLiquids1[i], 2, curLev);
		else if (curLev == 3)
			GAME->CreateLiquid(GAME->GetTileManager()->vLiquids1[i], 0, curLev);
		else if (curLev == 4)
			GAME->CreateLiquid(GAME->GetTileManager()->vLiquids1[i], 10000, curLev);
	}
}

void GameTimeAttack::SetPlayerPowerUps(const int dam, const int hp, const float speed)
{
	GAME->GetPlayer()->SetDamage(GAME->GetPlayer()->GetDamage() + dam);
	GAME->GetPlayer()->SetCurrentHP(GAME->GetPlayer()->GetCurrentHP() + hp);
	GAME->GetPlayer()->SetMaxSpeed(GAME->GetPlayer()->GetMaxSpeed() + speed);
}

// Level transition functions
int GameTimeAttack::GetTransitionID()
{
	return iTransitionID;
}
void GameTimeAttack::SetTransitionID(int ID)
{
	iTransitionID = ID;
}
void GameTimeAttack::PlayDeathTransition()
{
	SetTransition(true);
	SetTransitionID(DEATH);
	SetFadeoutDuration(3.0f); // 3.0 seconds for transition
}
void GameTimeAttack::PlayLoseTransition()
{
	SetTransition(true);
	SetTransitionID(LOSE);
	IsGameOver(true);
	SetFadeoutDuration(8.0f); // 6 seconds for transition
	GAME->SetTimeAttackLevel(1);
	GAME->ResetTimeAttackTime();
	GAME->PauseTimeAttack(true);
}
void GameTimeAttack::PlayWinTransition()
{
	SetTransition(true);
	SetVictory(true);
	SetTransitionID(WIN);
	GAME->SetTimeAttackLevel(GAME->GetTimeAttackLevel() + 1);
	SetFadeoutDuration(6.0f); // 6 seconds for level transition
}
bool GameTimeAttack::IsGameOver()
{
	return bGameOver;
}
void GameTimeAttack::IsGameOver(bool gameover)
{
	bGameOver = gameover;
}

bool GameTimeAttack::GetTransition()
{
	return bTransition;
}
void GameTimeAttack::SetTransition(bool transition)
{
	bTransition = transition;
}
bool GameTimeAttack::IsFadeoutFinished()
{
	return GetFadeoutTimer() >= GetFadeoutDuration() ? true : false;
}
float GameTimeAttack::GetFadeoutTimer()
{
	return fFadeoutTimer;
}
void GameTimeAttack::SetFadeoutTimer(float time)
{
	fFadeoutTimer = time;
}
float GameTimeAttack::GetFadeoutDuration()
{
	return fFadeoutDuration;
}
void GameTimeAttack::ResetFadeoutTimer()
{
	fFadeoutTimer = 0.0f;
}
void GameTimeAttack::SetFadeoutDuration(float time)
{
	fFadeoutDuration = time;
}
void GameTimeAttack::RenderFadeout()
{
	// Render Fadeout screen after three seconds of transition
	float time = (GetFadeoutTimer() - (GetFadeoutDuration() - 3.0f));
	if (time > 0.0f)
	{
		unsigned char alpha;
		if ((time * 200) > 255)
			alpha = 255;
		else
			alpha = (unsigned char)(time * 200);

		// Draw black screen
		GRAPHICS->DrawRectangle(SGD::Rectangle(0.0f, 0.0f, GAME->GetScreenWidth(), GAME->GetScreenWidth()), SGD::Color(alpha, 0, 0, 0));
	}
}

void GameTimeAttack::SetVictory(bool win)
{
	bVictory = win;
}

void GameTimeAttack::NextLevel()
{
	if (GAME->GetTimeAttackLevel() > 4)
	{
		GAME->SetBeatTimeAttack(true);
		GAME->AddHighScore(GAME->GetTimeAttackTime());
		GAME->SetTimeAttackLevel(1);
		GAME->ResetTimeAttackTime();
		GAME->PauseTimeAttack(true);
		GAME->ChangeState(CreditsState::GetInstance());
	}
	else
		GAME->ChangeState(GameTimeAttack::GetInstance());
}
void GameTimeAttack::PlayerDeath()
{
	SGD::Point pos;

	if (GAME->GetCheckpoint())
	{
		pos = GAME->GetCheckpointLoc();
		GAME->GetPlayer()->SetPosition(pos);
	}
	else
	{
		pos = GAME->GetPlayer()->GetSpawnLoc();
		GAME->GetPlayer()->SetPosition(pos);
	}

	// Unload Objects
	GAME->GetEntityManager().RemoveAll();

	LoadObjects(GAME->GetCurrentObjects());
	GAME->GetPlayer()->SetSpawnLoc(pos);
}

// Render functions
void GameTimeAttack::RenderTimer()
{
	float time = GAME->GetTimeAttackTime();
	int minutes, seconds, milliseconds;
	if (time > 0.0f)
	{
		seconds = (int)time;
		milliseconds = (int)((time - seconds) * 100);  // truncating to increments of 10 milliseconds
		minutes = seconds / 60;
		seconds %= 60;
	}
	else
	{
		milliseconds = seconds = minutes = 0;
	}
	std::wostringstream timer;
	if (minutes < 10)
		timer << "0";
	timer << minutes << ":";
	if (seconds < 10)
		timer << "0";
	timer << seconds << ":";
	if (milliseconds < 10)
		timer << "0";
	timer << milliseconds;

	GAME->GetBitmapFont()->Render(timer.str().c_str(), SGD::Point(35, 555), 0.5f, SGD::Color(0, 0, 0));
	GAME->GetBitmapFont()->Render(timer.str().c_str(), SGD::Point(40, 550), 0.5f, SGD::Color(255, 255, 255));
}
void GameTimeAttack::RenderBackground()
{
	float xPos = -Camera::GetInstance()->GetPosition().x;
	float xPos2;
	xPos *= 0.5;
	while (xPos <= -2408)
		xPos += 2408;
	if (xPos < -800)
		xPos2 = xPos + 2408;
	else
		xPos2 = xPos - 2408;

	if (hBackground != SGD::INVALID_HANDLE)
	{
		GRAPHICS->DrawTextureSection(hBackground, SGD::Point(xPos, 0), SGD::Rectangle(0.0f, 0.0f, 2408.0f, 600.0f));
		GRAPHICS->DrawTextureSection(hBackground, SGD::Point(xPos2, 0), SGD::Rectangle(0.0f, 0.0f, 2408.0f, 600.0f));
	}
}
void GameTimeAttack::RenderPauseMenu()
{
	// Display Pause Menu
	SGD::Point pCenter{ GAME->GetScreenWidth(), GAME->GetScreenHeight() };

	SGD::Point pBackgroundPauseLoc{ pCenter.x / 2 - 200, pCenter.y / 2 - 150 };
	GRAPHICS->DrawTexture(hPauseBackground, pBackgroundPauseLoc, 0.0f, SGD::Vector{}, SGD::Color{ 150, 255, 255, 255 });

	// Display menu options
	SGD::Color notSelected = { 255, 225, 100 };
	SGD::Color ResumeColor, QuitColor, ArrowColor;
	ResumeColor = QuitColor = ArrowColor = notSelected;

	SGD::Point pArrowLoc = {};

	switch (nCursor)
	{
	case 0:
		ArrowColor = ResumeColor = SGD::Color{ 255, 0, 0 };
		pArrowLoc = SGD::Point{ pCenter.x / 2 - 140, pCenter.y / 2 - 10 };
		break;
	case 1:
		ArrowColor = QuitColor = SGD::Color{ 255, 0, 0 };
		pArrowLoc = SGD::Point{ pCenter.x / 2 - 140, pCenter.y / 2 + 60 };
		break;
	}

	SGD::Point pPauseLoc{ pCenter.x / 2 - 90, pCenter.y / 2 - 90 };
	GAME->GetBitmapFont()->Render("GAME PAUSE", pPauseLoc, 0.5f, SGD::Color{ 255, 50, 50 });

	// Resume
	SGD::Point pResumeLoc{ pCenter.x / 2 - 85, pCenter.y / 2 - 10 };
	GAME->GetBitmapFont()->Render("Resume Game", pResumeLoc, 0.5f, ResumeColor);

	// Quit
	SGD::Point pQuitLoc{ pCenter.x / 2 - 115, pCenter.y / 2 + 60 };
	GAME->GetBitmapFont()->Render("Exit To Main Menu", pQuitLoc, 0.5f, QuitColor);

	// Arrow
	GAME->GetBitmapFont()->Render(">", pArrowLoc, 0.75f, ArrowColor);

	bInPauseMenu = true;
}
void GameTimeAttack::RenderGameOver()
{
	// Render Fadeout screen after three seconds of transition
	float time = (GetFadeoutTimer() - (GetFadeoutDuration() - 6.0f));
	if (time > 0.0f)
	{
		unsigned char alpha;
		if ((time * 200) > 255)
			alpha = 255;
		else
			alpha = (unsigned char)(time * 200);

		float left = 262.0f;
		float top = 285.0f;
		GAME->GetBitmapFont()->Render("Game Over", SGD::Point(left + 5, top + 5), 1.0f, SGD::Color(alpha, 0, 0, 0));
		GAME->GetBitmapFont()->Render("Game Over", SGD::Point(left, top), 1.0f, SGD::Color(alpha, 255, 50, 50));
	}
}
void GameTimeAttack::RenderLevelStart()
{
	float time = GetFadeoutTimer() / GetFadeoutDuration();
	float width = 5250 - (time * 8000);

	int num;
	for (unsigned int i = 0; i < 600; ++i)
	{
		num = i / 2;
		GRAPHICS->DrawLine(SGD::Point(0.0f, (float)i), SGD::Point(width - (float)num, (float)i), SGD::Color(0, 0, 0), 1);
	}

	unsigned char alpha;
	if (time < 0.65f)
		alpha = 255;
	else
		alpha = 255 - unsigned char((time - 0.6f) * 637.5f);

	// Draw sword
	GRAPHICS->DrawRectangle(SGD::Rectangle(0.0f, 395.0f, 600.0f, 426.0f), SGD::Color(alpha, 128, 128, 128));
	GRAPHICS->DrawRectangle(SGD::Rectangle(0.0f, 426.0f, 600.0f, 450.0f), SGD::Color(alpha, 76, 76, 76));
	GRAPHICS->DrawTexture(hSword, SGD::Point(578.0f, 402.5f), (SGD::PI / 4), SGD::Vector(20.5f, 20.5f), SGD::Color(alpha, 255, 255, 255));

	// Draw Text
	char* name;
	switch (GAME->GetTimeAttackLevel())
	{
	case 1:
		name = "Taelin Hills";
		break;
	case 2:
		name = "Soma Forest";
		break;
	case 3:
		name = "Kari Desert";
		break;
	case 4:
		name = "Shinto Caverns";
	}
	GAME->GetBitmapFont()->Render(name, SGD::Point(50, 415), 1.0f, SGD::Color(alpha, 0, 0, 0));
	GAME->GetBitmapFont()->Render(name, SGD::Point(45, 410), 1.0f, SGD::Color(alpha, 255, 255, 255));
}
void GameTimeAttack::RenderVictory()
{
	// Render Fadeout screen after three seconds of transition
	float time = (GetFadeoutTimer() - (GetFadeoutDuration() - 5.0f));
	if (time > 0.0f)
	{
		unsigned char alpha;
		if ((time * 200) > 255)
			alpha = 255;
		else
			alpha = (unsigned char)(time * 200);

		float left = 272.0f;
		float top = 285.0f;
		GAME->GetBitmapFont()->Render("You Win!", SGD::Point(left + 5, top + 5), 1.0f, SGD::Color(alpha, 0, 0, 0));
		GAME->GetBitmapFont()->Render("You Win!", SGD::Point(left, top), 1.0f, SGD::Color(alpha, 255, 50, 50));

		if (GAME->GetTimeAttackLevel() > 4)
		{
			GAME->GetBitmapFont()->Render("New Highscore!", SGD::Point(left + 5, top - 95), 1.0f, SGD::Color(alpha, 0, 0, 0));
			GAME->GetBitmapFont()->Render("New Highscore!", SGD::Point(left, top - 100), 1.0f, SGD::Color(alpha, 255, 50, 50));
		}
	}
}